#!/usr/bin/env python
import sys
import gtk
from HollyWidgets.TreeViewBased.TreeView import NodeCollection

class HTreeNode():

	def __init__( self, text = "", checked = False, opened_icon = None, closed_icon = None ):
                self.InnerIter   = None
                self.Store       = None
                self.Treeview    = None
                #properties
                self.icon        = closed_icon
                self.openedIcon  = opened_icon
                self.text        = text
                self.checked     = checked
                #
                self.ParentNode  = None
                self.IsExpanded  = False
		#nodes
                self.Nodes       = NodeCollection.NodeCollection()
                self.Init()
		
	def Init( self ):
		self.Nodes.connect( "NodeAdded"  , self.OnNodeAdded   )
                self.Nodes.connect( "NodeRemoved", self.OnNodeRemoved )
                self.Nodes.connect( "NodeUpdated", self.OnNodeUpdated )
	
	def selectAllChilds(self):
		Checked = True
		for nod in self.Nodes: 
			nod.selectAllChilds()
	        
	def deselectAllChilds( self ):
		Checked = False
		for nod in self.Nodes:
			nod.deselectAllChilds()
			
	def OnNodeUpdated(self, sender, args ):
		args.NewNode.Store     = args.OldNode.Store
		args.NewNode.InnerIter = args.OldNode.InnerIter
		args.NewNode.Treeview  = args.OldNode.Treeview
		args.NewNode.ParentNode= args.OldNode.ParentNode
		self.Store.set_value( args.OldNode.InnerIter, 0,  args.NewNode )
                
	def OnNodeRemoved( self, sender, args ):
		iter = args.Node.InnerIter
		self.Store.remove( iter )
                
	def OnNodeAdded( self, sender, args ):
		iter                 = self.Store.append( self.InnerIter, [ args.Node ] )
		args.Node.Store      = self.Store
		args.Node.InnerIter  = iter
		args.Node.Treeview   = self.Treeview
		args.Node.ParentNode = self
                
	def OnDrawItem( args ):
		#override this
		return None
		
	#properties
	def get_checked( self ):
		return self.checked
		

	def set_checked( self, value ):
		self.checked = value
		
	def get_text( self ):
		return self.text
		
	def set_text( self, value ):
		self.text = value
	
	def get_opened_icon( self ):
		return self.openedIcon
		
	def set_opened_icon( self, value ):
		self.openedIcon = value
		
	def get_icon( self ):
		return self.icon

	def set_icon( self, value ):
		self.icon = value

